/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.utilities;

import com.cashmate.hibernate.pojo.Branches;
import com.cashmate.hibernate.pojo.Usertypes;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.joda.time.DateTime;

/**
 *
 * @author Dine
 */
public class Utils {

    public static double roundToDecimals(double d, int c) {
        try {
            BigDecimal bd = new BigDecimal(Double.toString(d));
            bd = bd.setScale(c, BigDecimal.ROUND_HALF_UP);
            return bd.doubleValue();
        } catch (NumberFormatException nfe) {
            int temp = (int) ((d * Math.pow(10, c)));
            return (((double) temp) / Math.pow(10, c));
        }
    }

    public static String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    public static boolean isValid(String value, int min, int max) {
        if (value.length() < min || value.length() > max || value == null) {
            return false;
        }
        return true;
    }

    public static boolean isDigit(String value) {
        try {
            int test = Integer.parseInt(value);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    public static String prefixZero(int i) {
        if (i < 10) {
            return "0" + i;
        } else {
            return "" + i;
        }
    }

    public static String toOptionString(List<?> list, String type) {
        String opString = "{value:'";
        if (type.equals("usertypes")) {
            List<Usertypes> utList = (List<Usertypes>) list;
            for (Usertypes ut : utList) {
                opString += ut.getUsertypeid() + ":" + ut.getType() + ";";
            }
        } else if (type.equals("branch")) {
            List<Branches> brList = (List<Branches>) list;
            for (Branches b : brList) {
                opString += b.getBranchid() + ":" + b.getBranchname() + ";";
            }
        }
        return opString + "'}";
    }

    public static String segregate(String type, String word) {
        String letters = "";
        String numbers = "";
        for (char c : word.toCharArray()) {
            if (Character.isLetter(c)) {
                letters += c;
            } else if (Character.isDigit(c)) {
                numbers += c;
            }
        }
        if (type.contains("letters")) {
            return letters;
        }
        return numbers;
    }

    public static DateTime stringArrToDateTime(String[] arr) throws NumberFormatException {
        int[] intArr = new int[arr.length];
        int i = 0;
        for (String s : arr) {
            intArr[i] = Integer.parseInt(s);
            i++;
        }
        DateTime dt = new DateTime(intArr[2], intArr[0], intArr[1], 12, 0, 0, 0);
        return dt;
    }

    public static boolean isCutOff(DateTime dt) {
        if (dt.getDayOfMonth() % 6 == 0 || dt.getDayOfMonth() == dt.dayOfMonth().getMaximumValue()) {
            return true;
        }
        return false;
    }

    public static DateTime getLastCutOffDate(DateTime dccrdate) {
        DateTime dt = dccrdate;
        while (true) {
            dt = dt.minusDays(1);
            if (isCutOff(dt)) {
                break;
            }
        }
        return dt;
    }

    public static DateTime getNextCutOffDate(DCCRDate dccrdate) {
        String cutoff_s = "";
        switch (dccrdate.getCutoffSize()) {
            case 4:
                cutoff_s = dccrdate.getPaymentday4();
                break;
            case 5:
                cutoff_s = dccrdate.getPaymentday5();
                break;
            case 7:
                cutoff_s = dccrdate.getPaymentday7();
                break;
            default:
                cutoff_s = dccrdate.getPaymentday6();
                break;
        }
        DateTime temp_dt = stringArrToDateTime(cutoff_s.split("/")).plusDays(1);
        while (true) {
            if ((temp_dt.getDayOfMonth() % 6 == 0 && temp_dt.getDayOfMonth() != 30) || temp_dt.getDayOfMonth() == temp_dt.dayOfMonth().getMaximumValue()) {
                break;
            } else {
                temp_dt = temp_dt.plusDays(1);
            }
        }
        return temp_dt;
    }

    public static boolean isFirstCut(String date) {
        String[] s = date.split("/");
        if (s[1].equals("06") || s[1].equals("6")) {
            return true;
        }
        return false;
    }

    public static boolean isSecondCut(String date) {
        String[] s = date.split("/");
        if (s[1].equals("12")) {
            return true;
        }
        return false;
    }

    public static boolean isThirdCut(String date) {
        String[] s = date.split("/");
        if (s[1].equals("18")) {
            return true;
        }
        return false;
    }

    public static boolean isFourthCut(String date) {
        String[] s = date.split("/");
        if (s[1].equals("24")) {
            return true;
        }
        return false;
    }

    public static boolean isFifthCut(String date) {
        DateTime dt = Utils.stringArrToDateTime(date.split("/"));
        if (dt.getDayOfMonth() == dt.dayOfMonth().getMaximumValue()) {
            return true;
        }
        return false;
    }

    public static boolean isLesser(String date, int month, int year, int cutoffnum) {
        DateTime dt = new DateTime();
        try {
            dt = Utils.stringArrToDateTime(date.split("/"));
        } catch (NumberFormatException nfe) {
            return true;
        }
        DateTime max = new DateTime().withMonthOfYear(month).withYear(year);
        switch (cutoffnum) {
            case 1:
                if (dt.isBefore(max.withDayOfMonth(6)) || dt.isEqual(max.withDayOfMonth(6))) {
                    return true;
                }
                break;
            case 2:
                if (dt.isBefore(max.withDayOfMonth(12)) || dt.isEqual(max.withDayOfMonth(12))) {
                    return true;
                }
                break;
            case 3:
                if (dt.isBefore(max.withDayOfMonth(18)) || dt.isEqual(max.withDayOfMonth(18))) {
                    return true;
                }
                break;
            case 4:
                if (dt.isBefore(max.withDayOfMonth(24)) || dt.isEqual(max.withDayOfMonth(24))) {
                    return true;
                }
                break;
            case 5:
                if (dt.isBefore(max.withDayOfMonth(max.dayOfMonth().getMaximumValue())) || dt.isEqual(max.withDayOfMonth(max.dayOfMonth().getMaximumValue()))) {
                    return true;
                }
                break;
        }
        return false;
    }
}
